/**
 * $Revision: 1.0 $
 * $Author: Eric Yang $
 * $Date: Aug 30, 2009 9:42:20 PM $
 *
 * Author: Eric Yang
 * Date  : Aug 30, 2009 9:42:20 PM
 *
 */
package com.gooex.web.action.gbuy.product;

import java.util.List;
import org.c4j.string.StringTools;

import com.gooex.domain.base.Category;
import com.gooex.domain.base.Order;
import com.gooex.domain.base.Product;
import com.gooex.service.base.IOrderManager;
import com.gooex.service.base.IProductManager;
import com.gooex.web.action.BaseAnonymousAction;


/**
 * @author Eric Yang
 * @version 1.0
 */
public class ViewProduct extends BaseAnonymousAction {

	private IProductManager productManager;

	private IOrderManager orderManager;

	private String target;

	private String categoryId;

	private Category category;

	private Product product;

	private String act;

	private String id;

	private List<Order> orders;

	@Override
	public String execute() throws Exception {
		category = categoryManager.getCategory(categoryId);
		product = productManager.getProduct(id);
		if (product != null && category != null) {
			product.setCategory(category);
			orders = orderManager.getOrdersByProduct(product);
		}

		if ("cate".equals(act) || (StringTools.isBlankOrNull(categoryId) && StringTools.isBlankOrNull(id))) {
			return "category";
		}
		return SUCCESS;
	}

	public Category getCategory() {
		if (product != null) {
			category = product.getCategory();
			if (category == null) {
				category = productManager.getProduct(id).getCategory();
			}
		}
		return category;
	}

	public Product getProduct() {
		return product;
	}

	public boolean canEditProduct() {
		if (product == null || getCurrentUser() == null)
			return false;
		if (isSystemUser())
			return true;
		if (!product.isRaw())
			return false;
		if (!product.getUser().getId().equals(getCurrentUser().getId()))
			return false;
		if (orders != null && orders.size() > 0)
			return false;
		return true;
	}

	public void setProductManager(IProductManager productManager) {
		this.productManager = productManager;
	}

	public List<Order> getOrders() {
		return orders;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(String categoryId) {
		this.categoryId = categoryId;
	}

	public String getAct() {
		return act;
	}

	public void setAct(String act) {
		this.act = act;
	}

	public void setOrderManager(IOrderManager orderManager) {
		this.orderManager = orderManager;
	}

	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}

}
